<?php

namespace common\models\trade;

use Yii;
use common\helpers\Url;
use common\helpers\ColorHelper;

/**
 * This is the model class for table "{{%trade_paymode}}".
 *
 * @property int $id
 * @property string $name 名称
 * @property string $label 名称标签
 * @property string $icon 图标
 * @property string $color 16进制颜色值
 * @property string $class 样式名称
 * @property string $mark 备注
 * @property int $default 是否默认
 * @property int $status 启用状态：0未启用，1已启用
 * @property int $created_at 创建时间
 * @property int $updated_at 更新时间
 * @property int $deleted_at 删除时间
 */
class TradePaymode extends \common\db\ActiveRecord {

    const STATUS_ACTIVE = 10;
    const STATUS_DEFAULT = 0;

    /**
     * {@inheritdoc}
     */
    public static function tableName() {
        return '{{%trade_paymode}}';
    }

    /**
     * {@inheritdoc}
     */
    public function rules() {
        return [
            [['name', 'label'], 'required'],
            [['default', 'status', 'created_at', 'updated_at', 'deleted_at'], 'integer'],
            [['name'], 'string', 'max' => 20],
            [['label', 'class'], 'string', 'max' => 64],
            [['icon', 'mark'], 'string', 'max' => 255],
            [['color'], 'string', 'max' => 6],
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function attributeLabels() {
        return [
            'id' => 'ID',
            'name' => '名称',
            'label' => '名称标签',
            'icon' => '图标',
            'color' => '16进制颜色值',
            'class' => '样式名称',
            'mark' => '备注',
            'default' => '是否默认',
            'status' => '启用状态：0未启用，1已启用',
            'created_at' => '创建时间',
            'updated_at' => '更新时间',
            'deleted_at' => '删除时间',
        ];
    }

    public function fields() {
        return [
            'id',
            'name',
            'label',
            'icon' => function() {
                return Url::attach($this->icon);
            },
            'color' => function() {
                return ColorHelper::format($this->color);
            },
            'description' => 'mark',
            'default' => function() {
                return boolval($this->default);
            },
        ];
    }

    public function getTradePartner() {
        return $this->hasMany($class, $link);
    }

    public static function findByScope($scope) {
        TradePartner::find();
        $query = static::findActive('t', static::STATUS_ACTIVE);

//        $query->

        static::findAll([
            'deleted_at' => 0,
            'status' => TradePaymode::STATUS_ACTIVE,
        ]);
    }

}
